//*************************************************************************************
//* 作    者： zouhunter
//* 创建时间： 2021-10-08 02:34:40
//* 描    述： 文本提示

//* ************************************************************************************
using System;
using UnityEngine;
using System.Collections.Generic;

public class InfoNotieAgent : AgentContext<InfoNotieAgent>
{
    protected Dictionary<int, Tuple<string, float, float>> m_keyMap;
    protected InfoNoticeBehaviour m_noticeBehaviour;
    protected Collider m_lastHitLeft;
    protected Collider m_lastHitRight;
    protected float m_delyTimer;
    protected int m_delyHighlightTarget;

    protected override void OnCreate()
    {
        base.OnCreate();
        var canvasItem = SourceAgent.Instance.LoadGameObjectInstence("NoticeCanvas");
        GameObject.DontDestroyOnLoad(canvasItem);
        m_noticeBehaviour = canvasItem.GetComponent<InfoNoticeBehaviour>();
        m_noticeBehaviour.gameObject.SetActive(false);
        AppFacade.RegistEvent<bool, RaycastHit>(ObserverId.RAYCAST_HIT, OnRayHit);
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        AppFacade.RemoveEvent<bool, RaycastHit>(ObserverId.RAYCAST_HIT, OnRayHit);
    }

    private void OnRayHit(bool left, RaycastHit hit)
    {
        var lastHit = left ? m_lastHitLeft : m_lastHitRight;
        if (lastHit && hit.collider == lastHit)
        {
            if (lastHit.gameObject.GetInstanceID() == m_delyHighlightTarget && Time.realtimeSinceStartup > m_delyTimer)
            {
                DoOnFocus(lastHit.gameObject);
                m_delyHighlightTarget = 0;
            }
            return;
        }
        if (lastHit)
        {
            m_lastHitLeft = null;
            m_noticeBehaviour.gameObject.SetActive(false);
        }
        if (hit.collider)
        {
            float delytime = 1;
            if (m_keyMap != null && m_keyMap.TryGetValue(hit.collider.gameObject.GetInstanceID(), out var tuple))
            {
                delytime = tuple.Item3;
                OnFocus(hit.collider.gameObject, delytime);
            }
            lastHit = hit.collider;
        }
        if (left)
        {
            m_lastHitLeft = lastHit;
        }
        else
        {
            m_lastHitRight = lastHit;
        }
    }

    public void OnFocus(GameObject target, float timeout = 1)
    {
        if (target && m_keyMap.ContainsKey(target.GetInstanceID()))
        {
            m_delyHighlightTarget = target.GetInstanceID();
            m_delyTimer = Time.realtimeSinceStartup + timeout;
        }
    }

    protected void DoOnFocus(GameObject target)
    {
        if (m_keyMap.TryGetValue(target.GetInstanceID(), out var tuple))
        {
            var text = LanguageAgent.GetText(tuple.Item1);
            m_noticeBehaviour.SetText(text);
            m_noticeBehaviour.SetPosition(target.transform.position, tuple.Item2);
            m_noticeBehaviour.gameObject.SetActive(true);
        }
    }

    public void RemoveInstance(int instance)
    {
        if (m_keyMap != null)
        {
            m_keyMap.Remove(instance);
        }
    }

    public void ClearHighlight()
    {

    }

    public void RegistInstance(int instance, string key, float offset, float delyTime = 1)
    {
        if (m_keyMap == null)
        {
            m_keyMap = new Dictionary<int, Tuple<string, float, float>>();
        }
        m_keyMap[instance] = new Tuple<string, float, float>(key, offset, delyTime);
    }

    public void ClearRegisted()
    {
        if (m_keyMap != null)
            m_keyMap.Clear();
    }
}
